
package session.TX;

import Helper.Helper_Survey;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Query;

@Stateful
public class SurveyBean implements
        SurveyBeanRemote {
    @PersistenceContext()
    EntityManager em;
    StakeholderTier stakeholderTier;
    Stakeholder stakeholder;
    Places place;
    

    public SurveyBean() {}
    
    public Places getSpecificPlace(Long outletId) {

        Query q = em.createQuery("SELECT p FROM Places p");
        Places result = new Places();
        for(Object o: q.getResultList()){
            Places r = (Places)o;
        if (outletId == r.getOutlet().getId()) 
            result = r;
        }

        return result;
    }
    
    public int getTotalNoOfReviews(Places place) {
        int count = 0;
        for (Object o: place.getReviews()) {
            count++;
        }
        return count;
    }
    
    public int getNoOfReviews(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        
        List<Reviews> reviewList = place.getReviews();
        
        for (Reviews r: reviewList) {
            
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            count++;
        }
        }
        return count;
    }
    
    public Places getAllPlaces() {
        Query q = em.createQuery("SELECT p FROM Places p");
        Places result = new Places();
        for(Object o: q.getResultList()){
            Places r = (Places)o;
                result = r;
    }
        return result;
    }
 
    public Double getAmbienceResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        
        List<Reviews> reviewList = place.getReviews();
        
        for (Reviews r: reviewList) {
            
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getAmbience();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
    
    public Double getFoodResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        
        List<Reviews> reviewList = place.getReviews();
        
        for (Reviews r: reviewList) {
            
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getFood();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
    
    public Double getFoodResultAll(Places place) {
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            result = result + r.getFood();
            count++;
        }
        Double score = result/count;
        return score;
    }
    
    public Double getServiceResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        
        List<Reviews> reviewList = place.getReviews();
        
        for (Reviews r: reviewList) {
            
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getService();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
      
     public Double getServiceResultAll(Places place) {
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            result = result + r.getService();
            count++;
        }
        Double score = result/count;
        return score;
    }
     

        public Double getPricingResult(Places place, int month, int year) {
        Calendar calendar = null;
        int count = 0;
        double result = 0;
        
        List<Reviews> reviewList = place.getReviews();
        
        for (Reviews r: reviewList) {
            
            if (month == r.getReviewDate().get(calendar.MONTH) && year == r.getReviewDate().get(calendar.YEAR)){
            result = result + r.getPricing();
            count++;
        }
        }
        Double score = result/count;
        return score;
    }
        
     public Double getPricingResultAll(Places place) {
        int count = 0;
        double result = 0;
        for (Object o: place.getReviews()) {
            Reviews r = (Reviews)o;
            result = result + r.getPricing();
            count++;
        }
        Double score = result/count;
        return score;
    }


    @Remove
    public void remove() {
        System.out.println("SurveyBean:remove()");
    }

   

}